﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Zhjt.Comm.Lib.Tools.WebTools
{
    public class CookieManager
    {
        /// <summary>
        /// 写入cookie
        /// </summary>
        /// <param name="cookieKey"></param>
        /// <param name="value"></param>
        public static void SetCookie(string cookieKey, string value)
        {
            SetCookie(cookieKey, value, 0);
        }
        /// <summary>
        /// 写入cookie
        /// </summary>
        /// <param name="cookieKey"></param>
        /// <param name="value"></param>
        /// <param name="secondExpires">多少秒后,cookie过期</param>
        public static void SetCookie(string cookieKey, string value, int secondExpires)
        {
            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookieKey];
           
            if (null == cookie)
            {
                cookie = new HttpCookie(cookieKey);
               
            }
            cookie.Path = "/";
          //  cookie.Domain = System.Web.HttpContext.Current.Request.Url.Authority;




            if (secondExpires > 0) cookie.Expires = DateTime.Now.AddSeconds(secondExpires);
            cookie.Value = value;
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
        }
        ///// <summary>
        ///// 写入cookie
        ///// </summary>
        ///// <param name="cookieKey"></param>
        ///// <param name="value"></param>
        ///// <param name="secondExpires">多少秒后,cookie过期</param>
        //public static void SetCookie(string cookieKey, string value, int secondExpires,
        //    SameSiteMode sameSite = SameSiteMode.None, bool requireSSL = false)
        //{
        //    string sameSiteValue = string.Empty;
        //    string secureValue = string.Empty;
        //    switch (sameSite)
        //    {
        //        case SameSiteMode.Strict:
        //            sameSiteValue = " SameSite=Strict;";
        //            break;
        //        case SameSiteMode.Lax:
        //            sameSiteValue = " SameSite=Lax;";
        //            break;
        //        case SameSiteMode.None:
        //        default:
        //            sameSiteValue = " SameSite=None;";
        //            break;
        //    }
        //    if (requireSSL)
        //    {
        //        secureValue = " Secure";
        //    }

        //    string date ="";
        //    if (secondExpires != 0)
        //    {
        //        date =$"Expires={DateTime.Now.AddSeconds(secondExpires).ToFileTimeUtc().ToString()}";
        //    }
        //    System.Web.HttpContext.Current.Response.Headers.Add("set-cookie", string.Format($"{cookieKey}={value};{date}; path=/;{sameSiteValue}{secureValue}"));
        //}
        public enum SameSiteMode
        {
            Strict,
            Lax,
            None
        }
    /// <summary>
    /// 获取cookie
    /// </summary>
    /// <param name="cookieKey"></param>
    /// <returns></returns>
    public static string GetCookie(string cookieKey)
        {
            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookieKey];
            if (null != cookie)
            {
                return cookie.Value;
            }
            return "";
        }
        ///// <summary>
        ///// 删除指定cookie
        ///// </summary>
        ///// <param name="cookieKey"></param>
        //public static void Remove(string cookieKey)
        //{
        //    HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookieKey];
        //    cookie.Expires= DateTime.Now.AddDays(-1);
        //    cookie.Value = "";
        // //   System.Web.HttpContext.Current.Response.Headers.("Set-Cookie", response.getHeader("Set-Cookie") + ";SameSite=strict");
        //    System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
        //}
    }
}
